/**
 * async await
 * 
 * 
 */


const posts=[
    {title:"Post one",body:"this is post one"},
    {title:"Post two",body:"this is post two"}
];


function getPosts(){
    setTimeout(()=>{
        let output='';
        posts.forEach(post=>{
            output+=`<li>${post.title}</li>`
        });
        document.body.innerHTML=output;
    },1000)
}

function createPost(post){
    //实例化promise对象
    return new Promise((resovle,reject)=>{
        setTimeout(()=>{
            posts.push(post);

            const error=false;

            if(!error){
                resovle();
            }else{
                reject("Error:something went wrong!")
            }
        },2000)
    })
}

async function init(){
    await createPost({title:"Post Three",body:"this is post three"})
        .then(getPosts)
        .catch(err=>console.log(err))
    getPosts();
}
init();

async function fetchusers(){
    const res= await fetch
    ("https://jsonplaceholder.typicode.com/users");
    const data =await res.json();
    console.log(data);
}
fetchusers()